package com.changgou.seckill.controller;

import com.changgou.entity.Result;
import com.changgou.seckill.config.TokenDecode;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 秒杀下单业务controller
 */
@RestController
@RequestMapping("/seckillorder")
public class SeckillOrderController {

    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private SeckillOrderService seckillOrderService;

    /**
     * 秒杀下单
     * @param time  下单的秒杀时间场次
     * @param id    下单的商品id
     */
    @GetMapping("/add")
    public Boolean add(@RequestParam(value = "time") String time, @RequestParam(value = "id")Long id) {
        //1. 获取当前登录用户的用户名
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");

        //2. 调用service下单方法
        Boolean flag = seckillOrderService.add(time, id, username);
        return flag;
    }

}
